<?php
class order_model extends jqxGrid_CI_Model {

    function __construct()
    {
        parent::__construct();
        
    }
    
    function get($OrderID)
    {
        $query = $this->db->query("
            SELECT dtb_orders.*,dtb_product.`Amount` as PAmount,dtb_product.Sold as Sold
            FROM `dtb_orders`,dtb_product
            WHERE `OrderID`='$OrderID'
                AND `dtb_orders`.ProductID=dtb_product.ProductID
        ");
        return $query->result();
    }
    function getOrderDetail($OrderID){
        $query = $this->db->query("
            SELECT
            dtb_product.ProductID,
            dtb_product.ProductName,
            dtb_product.Amount,
            dtb_product.Sold,
            dtb_order_details.Quantity,
            dtb_orders.Status
            FROM
            dtb_orders
            Inner Join dtb_order_details ON dtb_orders.OrderID = dtb_order_details.OrderID 
            Inner Join dtb_product ON dtb_order_details.ProductID = dtb_product.ProductID
            WHERE
            dtb_orders.OrderID =  '$OrderID'
        ");
        return $query->result();
        

    }
    function delete($OrderID){
        $query = "
            UPDATE `dtb_orders` 
            SET `Delete` = now()
            WHERE `OrderID`='$OrderID'
        ";
        $this->db->query($query);
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
        
    }
    
    function restore($OrderID){
        $query = "
            UPDATE `dtb_orders` 
            SET     `Delete`    =   null
            WHERE   `OrderID`        =   '$OrderID'
        ";
        $this->db->query($query);
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    
    function changeStatus($OrderID,$Status,$type){
        $query = "
            UPDATE dtb_orders 
                INNER JOIN dtb_product ON dtb_orders.ProductID=dtb_product.ProductID
            SET 
                    dtb_orders.Status='$Status'";
        if($type=="+")
            $query.="
                    ,dtb_product.Sold=dtb_product.Sold+dtb_orders.Amount "; 
        elseif($type=="-")
            $query.="
                    ,dtb_product.Sold=dtb_product.Sold-dtb_orders.Amount ";
        $query.="
                WHERE dtb_orders.OrderID='$OrderID'

        ";
        $this->db->query($query);
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count>0) return true;
        return false;
    }
    
    function insert($params){
        $this->db->set('Insert', 'NOW()', FALSE);
        $this->db->insert('dtb_orders', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    function update($OrderID,$params){
        $this->db->where(array('OrderID'=> $OrderID,'Delete'=> null));
        $this->db->set('Update', 'NOW()', FALSE);
        $this->db->update('dtb_orders', $params); 
        $count = $this->db->affected_rows(); //should return the number of rows affected by the last query
        if($count==1) return true;
        return false;
    }
    
    function gettotal_rows(){
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS `dtb_orders`.*,dtb_product.ProductName,dtb_customer.CustomerName
            FROM dtb_customer,`dtb_orders`
                LEFT JOIN dtb_product ON
                    (
                        dtb_orders.ProductID=dtb_product.ProductID
                    )
            ";
        $strWhere="
            WHERE  dtb_orders.CustomerID=dtb_customer.CustomerID
            ";
        $strOrderBy="Order By `Insert` DESC ";
        $strGroupBy="";
        
        $config=array(
            "strQuery"=>$strQuery,
            "strWhere"=>$strWhere,
            "strOrderBy"=>$strOrderBy,
            "strGroupBy"=>$strGroupBy,
            "usinglimit"=>true,
            "fields"=>array(
                "CustomerID"=>"dtb_orders.CustomerID",
                "CustomerName"=>"dtb_customer.CustomerName",
                "Order"=>"dtb_orders.OrderID",
                "OrderID"=>"dtb_orders.OrderID",
                "Status"=>"dtb_orders.Status",
                "Insert"=>"dtb_orders.Insert",
                "Update"=>"dtb_orders.Update",
                "ShipPhone"=>"OrderDetail",
                
            )
        );
        $this->init($config);
        return $this->jqxBinding();
    }
    function gettotal_rows_detail($OrderID=null){
        $strQuery="
            SELECT SQL_CALC_FOUND_ROWS `dview_order_detail`.*
            FROM `dview_order_detail`
            WHERE `OrderID`='$OrderID' 
            ";
        $this->__new($strQuery);
        return $this->jqxBinding();
    }
    
}
?>
